package com.qiwo.webshow.controller;


import java.util.Arrays;

import com.qiwo.webshow.domain.ColumnMenuDO;
import com.qiwo.webshow.service.ColumnService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.base.AdminBaseController;
import com.qiwo.webshow.domain.BannerDO;
import com.qiwo.webshow.service.BannerService;
import com.qiwo.common.utils.Result;

/**
 * 
 * <pre>
 * banner图管理
 *  @author  chenwenchao
 *  @date 2018-10-22 21:27:24
 * </pre>
 * <small> 2018-10-22 21:27:24 | chenwenchao</small>
 */
@Controller
@RequestMapping("/webshow/banner")
public class BannerController extends AdminBaseController {
	@Autowired
	private BannerService bannerService;
	@Autowired
	private ColumnService columnService;
	@GetMapping()
	@RequiresPermissions("webshow:banner:banner")
	String Banner(Model model){
		Wrapper<ColumnMenuDO> wrapper = new EntityWrapper<ColumnMenuDO>();
		Page<ColumnMenuDO> columnMenuDOPage=new Page<>();
		columnMenuDOPage.setSize(100);
		columnMenuDOPage.setCurrent(0);
		Page<ColumnMenuDO> page = columnService.selectPage(columnMenuDOPage, wrapper);
		model.addAttribute("columnList",page.getRecords());
		return "webshow/banner/banner";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("webshow:banner:banner")
	public Result<Page<BannerDO>> list(BannerDO bannerDTO){
        Wrapper<BannerDO> wrapper = new EntityWrapper<BannerDO>().orderBy("typecode", false).orderBy("sort",true);
      if(bannerDTO!=null){
		if(bannerDTO.getTypecode()!=null){
			wrapper.eq("typecode",bannerDTO.getTypecode());
		}
	  }
        Page<BannerDO> page = bannerService.selectPage(getPage(BannerDO.class), wrapper);
      for(BannerDO bannerDO:page.getRecords()){
      	if(bannerDO.getTypecode()!=null){
      		ColumnMenuDO columnMenuDO=columnService.findOneByKv(bannerDO.getTypecode());
			bannerDO.setTypeName(columnMenuDO.getName());
		}
	  }
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("webshow:banner:add")
	String add(Model model){
		Wrapper<ColumnMenuDO> wrapper = new EntityWrapper<ColumnMenuDO>();
		Page<ColumnMenuDO> columnMenuDOPage=new Page<>();
		columnMenuDOPage.setSize(100);
		columnMenuDOPage.setCurrent(0);
		Page<ColumnMenuDO> page = columnService.selectPage(columnMenuDOPage, wrapper);
		model.addAttribute("columnList",page.getRecords());
	    return "webshow/banner/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("webshow:banner:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		Wrapper<ColumnMenuDO> wrapper = new EntityWrapper<ColumnMenuDO>();
		Page<ColumnMenuDO> columnMenuDOPage=new Page<>();
		columnMenuDOPage.setSize(100);
		columnMenuDOPage.setCurrent(0);
		Page<ColumnMenuDO> page = columnService.selectPage(columnMenuDOPage, wrapper);
		model.addAttribute("columnList",page.getRecords());
		BannerDO banner = bannerService.selectById(id);
		model.addAttribute("banner", banner);
	    return "webshow/banner/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("webshow:banner:add")
	public Result<String> save( BannerDO banner){
		bannerService.insert(banner);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("webshow:banner:edit")
	public Result<String>  update( BannerDO banner){
		boolean update = bannerService.updateById(banner);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("webshow:banner:remove")
	public Result<String>  remove( Long id){
		bannerService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("webshow:banner:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		bannerService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
